#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <conio.h>

typedef struct book
{
	int index;
	char isbn[10];
	char name[20];
	char author[20];
	char year[10];  
}Book;

//	//int  index;  int	 year;

void menu(FILE *);
void add(FILE *);
int find(FILE *);
int update_index(FILE *,Book );
//void change(FILE *);
//void del(FILE *);
void printTheSelection(FILE *);
void printBook(FILE *);

int *iFileSize,*iRecordSize,*iTotalRecords;

void main()
{
FILE *pb;
Book DemoRecord={-1,"","","",""};
char fileName[20];

iFileSize=(int*)calloc(1,sizeof(int));
iRecordSize=(int*)calloc(1,sizeof(int));
iTotalRecords=(int*)calloc(1,sizeof(int));

printf("Enter Library File Name>");
scanf("%s",fileName);

//// NEED TO ASK IF CREATE A NEW FILE (LIBRARY) OR USING THE DEFULT ONE

pb=fopen(fileName,"r+b");
if(!pb)
{
	pb=fopen(fileName,"a+b");
	fwrite(&DemoRecord,sizeof(Book),1,pb);
	fclose(pb);
}

menu(pb);
}
	////////////////////////////////////////////////////////
	void menu(FILE *pb)
	{
	int respond=1;

	printf("----------------------------\n");
	printf("---  Library Main Menu   ---\n");
	printf("----------------------------\n");
	   while(respond)
	   {
		printf("Select Library Operation:\n\n");

		printf("1) Add new Book \n");
		printf("2) Update a Book \n");
		printf("3) Delete a Book \n");
		printf("4) Find a Book by Author \n");
		printf("5) List all Books \n");
		printf("\n");
		printf("0) Exit \n");
		printf("\n");

		do
		{
			printf("please select 0-5 : ");
			scanf("%d",&respond);
		}while ((respond<0)||(respond>5));

		switch(respond)
			{
				case 1: add(pb,&iFileSize);
					break;
			  //case 2: change(pb);
				 //  break;
			  // case 3: del(pb);
				 //  break;
				case 4:	find(pb);  //printTheSelection(pb);
					break;
				case 5:	printBook(pb);
					break;
				case 0: printf("Exit\n\n");
					return;
		   }
	   }
	}
	////////////////////////////////////////////////////////

	////////////////////////////////////////////////////////
	void printBook(FILE *pb)
	{
		Book curr;
	
			printf("\n");
			printf("Index \t\tAuthor \t\tBook Name \tISBN \t\tYear\n");

			rewind(pb);
			
			while(fread(&curr,sizeof(Book),1,pb))
			{
				printf("%d \t\t%s \t\t%s \t\t%s \t\t%s\n",curr.index,curr.author,curr.name,curr.isbn,curr.year);
			}

			printf("\n");

		menu(pb);
	}
	////////////////////////////////////////////////////////

	////////////////////////////////////////////////////////
	void printTheSelection(FILE * pb)
	{
		Book foundVal;
		if(find(pb))
			{
				fseek(pb,-sizeof(Book),SEEK_CUR);
				fread(&foundVal,sizeof(Book),1,pb);
				printf("Index \t\tAuthor \t\tBook Name \t\tISBN \t\tYear\n");
				printf("%5d %11s %21s %21s %11s\n",foundVal.index,foundVal.author,foundVal.name,foundVal.isbn,foundVal.year);
			}
		else
			printf("There is Not Such Record!\n");

		menu(pb);
	}
	////////////////////////////////////////////////////////

	////////////////////////////////////////////////////////
	void add(FILE *pb)
	{
		Book newRecord;

		fseek(pb,0,SEEK_END);
		*iFileSize=ftell(pb);
		*iRecordSize=sizeof(Book);
		*iTotalRecords=(*iFileSize)/(*iRecordSize);
		newRecord.index = *iTotalRecords;

		//puts(newRecord.index);

		printf("\nPlease Enter the Record Author Name: ");
		gets(newRecord.author);
		flushall();
		printf("\nPlease Enter the Record Book Name: ");
		gets(newRecord.name);
		flushall();
		printf("\nPlease Enter the Record ISBN: ");
		gets(newRecord.isbn);
		flushall();
		printf("\nPlease Enter the Record Book Year: ");
		gets(newRecord.year);

		newRecord.index = update_index(pb,newRecord);

		fwrite(&newRecord,sizeof(Book),1,pb);
		printf("\n");
		menu(pb);
	}
	////////////////////////////////////////////////////////

	////////////////////////////////////////////////////////
	int find(FILE *pb)
	{
		Book buf;
		char findValue[20];
		int flag =0;

		rewind(pb);

			flushall();

			printf("\n");
			printf("Please Enter the Author Name You Want to Look For: ");	
			scanf("%s",&findValue);
			while (fread(&buf,sizeof(Book),1,pb))
			{
				if(!strcmp(findValue,buf.author))
				{
					printf("%d \t\t\t%s \t\t%s \t\t%s \t\t%s\n",buf.index,buf.author,buf.name,buf.isbn,buf.year);
					flag = 1;
				}
			}

			if (flag==0) printf("There is Not Such Author!\n");
			printf("\n");

			return flag;

		menu(pb);
	}
	////////////////////////////////////////////////////////

	////////////////////////////////////////////////////////

	////////////////////////////////////////////////////////

	////////////////////////////////////////////////////////
	int update_index(FILE *pb,Book newRecord)
	{
		Book buf;
		int flag,new_index=0;
		
		rewind(pb);
		flushall();

		while (fread(&buf,sizeof(Book),1,pb))
		{
			flag = strcmp(newRecord.author,buf.author);
			switch(flag)
			{
			case 0:
				new_index=buf.index;
				continue;
			case 1:
				new_index=buf.index;
				continue;
			case -1:
				new_index= buf.index++;
				break;
			}

			printf("Flag:[%d] , index: [%d] | New Author: [%s] - Author: [%s]\n",flag,buf.index,newRecord.author,buf.author);

			return *iTotalRecords;
		}
	}
	////////////////////////////////////////////////////////


